package com.android.hidephotosplus.db;

import java.net.URLDecoder;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.util.Log;

import com.android.hidephotosplus.model.ModelAlbum;
import com.android.hidephotosplus.model.ModelMedia;
import com.android.hidephotosplus.model.ModelMediaOfAlbum;
import com.android.hidephotosplus.util.Config;
import com.android.hidephotosplus.util.Util;

public class DbProvider {

	private Context context;

	static DbProvider mDbProvider = null;

	public DbProvider(Context context) {
		this.context = context;
	}

	public static DbProvider getinstance(Context context) {
		if (mDbProvider == null) {
			synchronized (DbProvider.class) {
				mDbProvider = new DbProvider(context);
			}
		}
		return mDbProvider;
	}

	public ArrayList<ModelAlbum> getAllAlbum() {
		ArrayList<ModelAlbum> mArrAlbums = new ArrayList<ModelAlbum>();
		mArrAlbums.addAll(getAllAlbumPhotos(true));
		mArrAlbums.addAll(getAllAlbumVideos(true, getAllAlbumPhotos(true)));
		return mArrAlbums;
	}

	public ArrayList<ModelAlbum> getAllAlbumPhotos(boolean isAllAlbum) {
		ArrayList<ModelAlbum> mArrAlbums = new ArrayList<ModelAlbum>();
		String[] project = new String[] {
				" DISTINCT "
						+ MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
				MediaStore.Images.ImageColumns.BUCKET_ID };

		Cursor cursor = context.getContentResolver().query(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI, project, null,
				null,
				MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME + " asc");
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				ModelAlbum item = new ModelAlbum();
				item.setNameAlbum(cursor.getString(0));
				item.setIdAlbum(cursor.getLong(1));
				item.setMediaOfAlbum(getMediaOfAlbum(item.getIdAlbum()));
				if (isAllAlbum)
					item.setNumberMedia(numberPhoto(item.getIdAlbum())
							+ numberVideo(item.getIdAlbum()));
				else
					item.setNumberMedia(numberPhoto(item.getIdAlbum()));
				mArrAlbums.add(item);
			}
			tryClose(cursor);
		}
		return mArrAlbums;
	}

	public ArrayList<ModelAlbum> getAllAlbumVideos(boolean isAllAlbum,
			ArrayList<ModelAlbum> arrAlbumsPhoto) {
		ArrayList<ModelAlbum> mArrAlbums = new ArrayList<ModelAlbum>();
		String[] projectVideo = new String[] {
				" DISTINCT "
						+ MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME,
				MediaStore.Video.VideoColumns.BUCKET_ID };
		Cursor cursorVideo = context.getContentResolver().query(
				MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projectVideo,
				null, null,
				MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME + " asc");
		if (cursorVideo != null && cursorVideo.getCount() > 0) {
			while (cursorVideo.moveToNext()) {
				ModelAlbum item = new ModelAlbum();
				boolean isExists = false;
				if (arrAlbumsPhoto != null)
					for (ModelAlbum mItem : arrAlbumsPhoto) {
						if (mItem.getIdAlbum() == cursorVideo.getLong(1)) {
							isExists = true;
						}
					}
				if (!isExists) {
					item.setNameAlbum(cursorVideo.getString(0));
					item.setIdAlbum(cursorVideo.getLong(1));
					item.setMediaOfAlbum(getMediaOfAlbumVideo(item.getIdAlbum()));
					if (isAllAlbum)
						item.setNumberMedia(numberPhoto(item.getIdAlbum())
								+ numberVideo(item.getIdAlbum()));
					else
						item.setNumberMedia(numberVideo(item.getIdAlbum()));
					mArrAlbums.add(item);
				}

			}
		}
		tryClose(cursorVideo);
		return mArrAlbums;
	}

	private void setupPathIdMedia(ModelAlbum item, int count, Cursor cursor,
			boolean isVideo) {

		ModelMediaOfAlbum mediaOfAlbum = new ModelMediaOfAlbum();
		while (cursor.moveToNext()) {
			if (cursor.getCount() == 1) {
				long idMedia = cursor.getLong(1);
				mediaOfAlbum.setIdMedia3(idMedia);
				mediaOfAlbum.setPathMediaStock3(cursor.getString(0));
				mediaOfAlbum.setVideo3(isVideo);
			} else if (cursor.getCount() == 2) {
				if (count == 0) {
					long idMedia = cursor.getLong(1);
					mediaOfAlbum.setIdMedia3(idMedia);
					mediaOfAlbum.setPathMediaStock3(cursor.getString(0));
					mediaOfAlbum.setVideo3(isVideo);
				} else if (count == 1) {
					long idMedia = cursor.getLong(1);
					mediaOfAlbum.setIdMedia2(idMedia);
					mediaOfAlbum.setPathMediaStock2(cursor.getString(0));
					mediaOfAlbum.setVideo2(isVideo);
				}
			} else if (cursor.getCount() == 3) {
				if (count == 0) {
					long idMedia = cursor.getLong(1);
					mediaOfAlbum.setIdMedia3(idMedia);
					mediaOfAlbum.setPathMediaStock3(cursor.getString(0));
					mediaOfAlbum.setVideo3(isVideo);
				} else if (count == 1) {
					long idMedia = cursor.getLong(1);
					mediaOfAlbum.setIdMedia2(idMedia);
					mediaOfAlbum.setPathMediaStock2(cursor.getString(0));
					mediaOfAlbum.setVideo2(isVideo);
				} else if (count == 2) {
					long idMedia = cursor.getLong(1);
					mediaOfAlbum.setIdMedia(idMedia);
					mediaOfAlbum.setPathMediaStock(cursor.getString(0));
					mediaOfAlbum.setVideo(isVideo);
				}
			}
			count++;
		}
		item.setMediaOfAlbum(mediaOfAlbum);
	}

	public int numberPhoto(long idAlbum) {
		int result = 0;
		String project[] = new String[] { " count(*) " };
		Cursor mCursor = context.getContentResolver().query(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				project,
				MediaStore.Images.ImageColumns.BUCKET_ID + " = " + idAlbum
						+ "  ", null, null);
		if (mCursor != null) {
			while (mCursor.moveToNext()) {
				result = mCursor.getInt(0);
			}
		}
		tryClose(mCursor);
		return result;
	}

	public int numberVideo(long idAlbum) {
		int result = 0;
		String project[] = new String[] { " count(*) " };
		Cursor mCursor = context.getContentResolver().query(
				MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
				project,
				MediaStore.Video.VideoColumns.BUCKET_ID + " = " + idAlbum
						+ "  ", null, null);
		if (mCursor != null) {
			while (mCursor.moveToNext()) {
				result = mCursor.getInt(0);
			}
		}
		tryClose(mCursor);
		return result;
	}

	public ArrayList<ModelMedia> getAllMedias(long idAlbum) {
		ArrayList<ModelMedia> arrMedias = new ArrayList<ModelMedia>();
		arrMedias.addAll(getAllPhotos(idAlbum));
		arrMedias.addAll(getAllVideos(idAlbum));
		return arrMedias;
	}

	public ArrayList<ModelMedia> getAllPhotos(long idAlbum) {
		ArrayList<ModelMedia> ArrMedias = new ArrayList<ModelMedia>();
		Cursor cursor = context.getContentResolver().query(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				new String[] { MediaStore.Images.ImageColumns._ID,
						MediaStore.Images.ImageColumns.DATA,
						MediaStore.Images.ImageColumns.DISPLAY_NAME,
						MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
						MediaStore.Images.ImageColumns.DATE_MODIFIED },
				" " + MediaStore.Images.ImageColumns.BUCKET_ID + " = '"
						+ idAlbum + "'  ", null,
				MediaStore.Images.ImageColumns.DATE_MODIFIED + " DESC ");

		if (cursor != null && cursor.getCount() > 0)
			while (cursor.moveToNext()) {
				ModelMedia item = new ModelMedia();
				item.setIdMedia(cursor.getLong(0));
				item.setNameAlbum(cursor.getString(3));
				item.setPathMediaStock(cursor.getString(1));
				item.setNameMedia(cursor.getString(2));
				item.setDate(cursor.getString(4));
				item.setVideo(false);
				ArrMedias.add(item);
			}
		tryClose(cursor);
		return ArrMedias;
	}

	public ArrayList<ModelMedia> getAllVideos(long idAlbum) {
		ArrayList<ModelMedia> ArrMedias = new ArrayList<ModelMedia>();
		Cursor mCursorVideo = context.getContentResolver().query(
				MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
				new String[] { MediaStore.Video.VideoColumns._ID,
						MediaStore.Video.VideoColumns.DATA,
						MediaStore.Video.VideoColumns.DISPLAY_NAME,
						MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME,
						MediaStore.Video.VideoColumns.DATE_MODIFIED },
				" " + MediaStore.Video.VideoColumns.BUCKET_ID + " = '"
						+ idAlbum + "'  ", null,
				MediaStore.Video.VideoColumns.DATE_MODIFIED + " DESC ");
		if (mCursorVideo != null && mCursorVideo.getCount() > 0)
			while (mCursorVideo.moveToNext()) {
				ModelMedia item = new ModelMedia();
				item.setIdMedia(mCursorVideo.getLong(0));
				item.setPathMediaStock(mCursorVideo.getString(1));
				item.setNameMedia(mCursorVideo.getString(2));
				item.setNameAlbum(mCursorVideo.getString(3));
				item.setDate(mCursorVideo.getString(4));
				item.setVideo(true);
				ArrMedias.add(item);
			}
		tryClose(mCursorVideo);
		return ArrMedias;
	}

	public void deletePhotosProvider(long idPhoto) {
		try {
			context.getContentResolver().delete(
					MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
					" " + MediaStore.Images.ImageColumns._ID + " = '" + idPhoto
							+ "' ", null);
		} catch (Exception e) {
		}
	}

	public void deleteVideosProvider(long idVideo) {
		try {
			context.getContentResolver().delete(
					MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
					" " + MediaStore.Video.VideoColumns._ID + " = '" + idVideo
							+ "' ", null);
		} catch (Exception e) {
		}
	}

	public void insertMedia(String namephoto, String Path, String date,
			boolean isVideo) {
		try {
			if (!isVideo) {
				ContentValues mContentValues = new ContentValues();
				mContentValues.put(MediaStore.Images.ImageColumns.DISPLAY_NAME,
						namephoto);
				mContentValues.put(MediaStore.Images.ImageColumns.DATA, Path);
				mContentValues.put(
						MediaStore.Images.ImageColumns.DATE_MODIFIED, date);
				mContentValues.put(MediaStore.Images.ImageColumns.MIME_TYPE,
						"image/*");
				context.getContentResolver().insert(
						MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
						mContentValues);
			} else {
				ContentValues mContentValues = new ContentValues();
				mContentValues.put(MediaStore.Video.VideoColumns.DISPLAY_NAME,
						namephoto);
				mContentValues.put(MediaStore.Video.VideoColumns.MIME_TYPE,
						"video/*");
				mContentValues.put(MediaStore.Video.VideoColumns.DATA, Path);
				mContentValues.put(MediaStore.Video.VideoColumns.DATE_MODIFIED,
						date);
				context.getContentResolver().insert(
						MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
						mContentValues);
			}
		} catch (Exception e) {
		}
	}

	public ArrayList<ModelAlbum> getAllAlbumByNameAlbum(String nameAlbum) {
		ArrayList<ModelAlbum> mArrAlbums = new ArrayList<ModelAlbum>();
		mArrAlbums.addAll(getAllAlbumPhotosByNameAlbum(true, nameAlbum));
		mArrAlbums.addAll(getAllAlbumVideosByNameAlbum(true, nameAlbum,
				mArrAlbums));
		return mArrAlbums;
	}

	public ArrayList<ModelAlbum> getAllAlbumPhotosByNameAlbum(
			boolean isAllAlbum, String nameAlbum) {
		ArrayList<ModelAlbum> mArrAlbums = new ArrayList<ModelAlbum>();

		String[] project = new String[] {
				" DISTINCT "
						+ MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
				MediaStore.Images.ImageColumns.BUCKET_ID };

		Cursor cursor = context.getContentResolver().query(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				project,
				" " + MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME
						+ " ='" + nameAlbum + "'  ", null,
				MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME + " asc");
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				ModelAlbum item = new ModelAlbum();
				item.setNameAlbum(cursor.getString(0));
				item.setIdAlbum(cursor.getLong(1));

				if (isAllAlbum)
					item.setNumberMedia(numberPhoto(item.getIdAlbum())
							+ numberVideo(item.getIdAlbum()));
				else
					item.setNumberMedia(numberPhoto(item.getIdAlbum()));

				Cursor cursor2 = context.getContentResolver().query(
						MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
						new String[] { MediaStore.Images.ImageColumns.DATA,
								MediaStore.Images.ImageColumns._ID },
						MediaStore.Images.ImageColumns.BUCKET_ID + " =?",
						new String[] { String.valueOf(item.getIdAlbum()) },
						MediaStore.Images.ImageColumns.DATE_MODIFIED
								+ " DESC limit 4");
				if (cursor2 != null && cursor2.getCount() > 0) {
					int count = 0;
					setupPathIdMedia(item, count, cursor2, false);
				}
				tryClose(cursor2);
				if (isAllAlbum) {
					Cursor cursorVideo1 = context.getContentResolver().query(
							MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
							new String[] { MediaStore.Video.VideoColumns.DATA,
									MediaStore.Video.VideoColumns._ID },
							MediaStore.Video.VideoColumns.BUCKET_ID + " =?",
							new String[] { String.valueOf(item.getIdAlbum()) },
							MediaStore.Video.VideoColumns.DATE_MODIFIED
									+ " DESC limit 3");
					ModelMediaOfAlbum mediaOfAlbum = item.getMediaOfAlbum();
					if (mediaOfAlbum.getPathMediaStock2() == null
							|| mediaOfAlbum.getPathMediaStock() == null) {
						if (cursorVideo1 != null && cursorVideo1.getCount() > 0) {
							if (cursorVideo1 != null
									&& cursorVideo1.getCount() > 0) {
								while (cursorVideo1.moveToNext()) {
									if (mediaOfAlbum.getPathMediaStock2() == null) {
										mediaOfAlbum.setIdMedia2(cursorVideo1
												.getLong(1));
										mediaOfAlbum
												.setPathMediaStock2(cursorVideo1
														.getString(0));
										mediaOfAlbum.setVideo2(true);
									} else if (mediaOfAlbum.getPathMediaStock() == null) {
										mediaOfAlbum.setIdMedia(cursorVideo1
												.getLong(1));
										mediaOfAlbum
												.setPathMediaStock(cursorVideo1
														.getString(0));
										mediaOfAlbum.setVideo(true);
									}
								}
							}
						}
						tryClose(cursorVideo1);
					}
				}
				mArrAlbums.add(item);

			}
			tryClose(cursor);
		}
		return mArrAlbums;
	}

	public ArrayList<ModelAlbum> getAllAlbumVideosByNameAlbum(
			boolean isAllAlbum, String nameAlbum,
			ArrayList<ModelAlbum> arrAlbumsPhoto) {
		ArrayList<ModelAlbum> mArrAlbums = new ArrayList<ModelAlbum>();
		String[] projectVideo = new String[] {
				" DISTINCT "
						+ MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME,
				MediaStore.Video.VideoColumns.BUCKET_ID };
		Cursor cursorVideo = context.getContentResolver().query(
				MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
				projectVideo,
				" " + MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME + " ='"
						+ nameAlbum + "'  ", null,
				MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME + " asc");
		if (cursorVideo != null && cursorVideo.getCount() > 0) {
			while (cursorVideo.moveToNext()) {

				ModelAlbum item = new ModelAlbum();

				boolean isExists = false;
				if (arrAlbumsPhoto != null)
					for (ModelAlbum mItem : arrAlbumsPhoto) {
						if (mItem.getIdAlbum() == cursorVideo.getLong(1)) {
							isExists = true;
						}
					}
				if (!isExists) {
					item.setNameAlbum(cursorVideo.getString(0));
					item.setIdAlbum(cursorVideo.getLong(1));
					if (isAllAlbum)
						item.setNumberMedia(numberPhoto(item.getIdAlbum())
								+ numberVideo(item.getIdAlbum()));
					else
						item.setNumberMedia(numberVideo(item.getIdAlbum()));

					Cursor cursorVideo1 = context.getContentResolver().query(
							MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
							new String[] { MediaStore.Video.VideoColumns.DATA,
									MediaStore.Video.VideoColumns._ID },
							MediaStore.Video.VideoColumns.BUCKET_ID + " =?",
							new String[] { String.valueOf(item.getIdAlbum()) },
							MediaStore.Video.VideoColumns.DATE_MODIFIED
									+ " DESC limit 4");
					if (cursorVideo1 != null && cursorVideo1.getCount() > 0) {
						int count = 0;
						setupPathIdMedia(item, count, cursorVideo1, true);
					}
					tryClose(cursorVideo1);
					mArrAlbums.add(item);
				}

			}

			tryClose(cursorVideo);
		}

		return mArrAlbums;
	}

	public ModelMediaOfAlbum getMediaOfAlbum(long idAlbum) {
		ModelMediaOfAlbum mediaOfAlbum = getMediaOfAlbumPhoto(idAlbum);

		if (mediaOfAlbum.getPathMediaStock4() == null
				|| Config.NONE.equals(mediaOfAlbum.getPathMediaStock4()))
			mediaOfAlbum = getMediaOfAlbumVideo(idAlbum);
		else {
			if (mediaOfAlbum.getPathMediaStock3() == null
					|| Config.NONE.equals(mediaOfAlbum.getPathMediaStock3())
					|| mediaOfAlbum.getPathMediaStock2() == null
					|| Config.NONE.equals(mediaOfAlbum.getPathMediaStock2())
					|| mediaOfAlbum.getPathMediaStock() == null
					|| Config.NONE.equals(mediaOfAlbum.getPathMediaStock())) {
				Cursor cursorVideo = context.getContentResolver().query(
						MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
						new String[] { MediaStore.Video.VideoColumns.DATA,
								MediaStore.Video.VideoColumns._ID },
						MediaStore.Video.VideoColumns.BUCKET_ID + " =?",
						new String[] { String.valueOf(idAlbum) },
						MediaStore.Video.VideoColumns.DATE_MODIFIED
								+ " DESC limit 3");
				if (cursorVideo != null && cursorVideo.getCount() > 0) {
					while (cursorVideo.moveToNext()) {
						if (mediaOfAlbum.getPathMediaStock3() == null
								|| Config.NONE.equals(mediaOfAlbum
										.getPathMediaStock3())) {
							mediaOfAlbum.setIdMedia3(cursorVideo.getLong(1));
							mediaOfAlbum.setPathMediaStock3(cursorVideo
									.getString(0));
							mediaOfAlbum.setVideo3(true);
							mediaOfAlbum.setIdMedia2(-1);
							mediaOfAlbum.setPathMediaStock2(Config.NONE);
							mediaOfAlbum.setVideo2(false);
							mediaOfAlbum.setIdMedia(-1);
							mediaOfAlbum.setPathMediaStock(Config.NONE);
							mediaOfAlbum.setVideo(false);
						} else if (mediaOfAlbum.getPathMediaStock2() == null
								|| Config.NONE.equals(mediaOfAlbum
										.getPathMediaStock2())) {
							mediaOfAlbum.setIdMedia2(cursorVideo.getLong(1));
							mediaOfAlbum.setPathMediaStock2(cursorVideo
									.getString(0));
							mediaOfAlbum.setVideo2(true);
							
							mediaOfAlbum.setIdMedia(-1);
							mediaOfAlbum.setPathMediaStock(Config.NONE);
							mediaOfAlbum.setVideo(false);
						} else if (mediaOfAlbum.getPathMediaStock() == null
								|| Config.NONE.equals(mediaOfAlbum
										.getPathMediaStock())) {
							mediaOfAlbum.setIdMedia(cursorVideo.getLong(1));
							mediaOfAlbum.setPathMediaStock(cursorVideo
									.getString(0));
							mediaOfAlbum.setVideo(true);
						}
					}
				}
				tryClose(cursorVideo);
			}

		}
		return mediaOfAlbum;
	}

	public ModelMediaOfAlbum getMediaOfAlbumPhoto(long idAlbum) {
		ModelMediaOfAlbum mediaOfAlbum = new ModelMediaOfAlbum();
		Cursor cursor = context.getContentResolver().query(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				new String[] { MediaStore.Images.ImageColumns.DATA,
						MediaStore.Images.ImageColumns._ID },
				MediaStore.Images.ImageColumns.BUCKET_ID + " =?",
				new String[] { String.valueOf(idAlbum) },
				MediaStore.Images.ImageColumns.DATE_MODIFIED + " DESC limit 4");

		if (cursor != null && cursor.getCount() == 0) {
			mediaOfAlbum.setPathMediaStock4(Config.NONE);
			mediaOfAlbum.setIdMedia4(-1);
			mediaOfAlbum.setVideo4(false);
			mediaOfAlbum.setPathMediaStock3(Config.NONE);
			mediaOfAlbum.setIdMedia3(-1);
			mediaOfAlbum.setVideo3(false);
			mediaOfAlbum.setPathMediaStock2(Config.NONE);
			mediaOfAlbum.setIdMedia2(-1);
			mediaOfAlbum.setVideo2(false);
			mediaOfAlbum.setPathMediaStock(Config.NONE);
			mediaOfAlbum.setIdMedia(-1);
			mediaOfAlbum.setVideo(false);
		} else {
			if (cursor != null && cursor.getCount() > 0) {
				while (cursor.moveToNext()) {
					if (mediaOfAlbum.getPathMediaStock4() == null
							|| Config.NONE.equals(mediaOfAlbum
									.getPathMediaStock4())) {

						mediaOfAlbum.setPathMediaStock4(cursor.getString(0));
						mediaOfAlbum.setIdMedia4(cursor.getLong(1));
						mediaOfAlbum.setVideo4(false);
						mediaOfAlbum.setPathMediaStock3(Config.NONE);
						mediaOfAlbum.setIdMedia3(-1);
						mediaOfAlbum.setVideo3(false);
						mediaOfAlbum.setPathMediaStock2(Config.NONE);
						mediaOfAlbum.setIdMedia2(-1);
						mediaOfAlbum.setVideo2(false);
						mediaOfAlbum.setPathMediaStock(Config.NONE);
						mediaOfAlbum.setIdMedia(-1);
						mediaOfAlbum.setVideo(false);
					} else if (mediaOfAlbum.getPathMediaStock3() == null
							|| Config.NONE.equals(mediaOfAlbum
									.getPathMediaStock3())) {
						mediaOfAlbum.setPathMediaStock3(cursor.getString(0));
						mediaOfAlbum.setIdMedia3(cursor.getLong(1));
						mediaOfAlbum.setVideo3(false);
						mediaOfAlbum.setPathMediaStock2(Config.NONE);
						mediaOfAlbum.setIdMedia2(-1);
						mediaOfAlbum.setVideo2(false);
						mediaOfAlbum.setPathMediaStock(Config.NONE);
						mediaOfAlbum.setIdMedia(-1);
						mediaOfAlbum.setVideo(false);
					} else if (mediaOfAlbum.getPathMediaStock2() == null
							|| Config.NONE.equals(mediaOfAlbum
									.getPathMediaStock2())) {
						mediaOfAlbum.setPathMediaStock2(cursor.getString(0));
						mediaOfAlbum.setIdMedia2(cursor.getLong(1));
						mediaOfAlbum.setVideo2(false);
						mediaOfAlbum.setPathMediaStock(Config.NONE);
						mediaOfAlbum.setIdMedia(-1);
						mediaOfAlbum.setVideo(false);
					} else if (mediaOfAlbum.getPathMediaStock() == null
							|| Config.NONE.equals(mediaOfAlbum
									.getPathMediaStock())) {
						mediaOfAlbum.setPathMediaStock(cursor.getString(0));
						mediaOfAlbum.setIdMedia(cursor.getLong(1));
						mediaOfAlbum.setVideo(false);
					}
				}
			}
			// tryClose(cursor);
			// if (mediaOfAlbum.getPathMediaStock3() == null
			// || Config.NONE.equals(mediaOfAlbum.getPathMediaStock3())
			// || mediaOfAlbum.getPathMediaStock2() == null
			// || Config.NONE.equals(mediaOfAlbum.getPathMediaStock2())
			// || mediaOfAlbum.getPathMediaStock() == null
			// || Config.NONE.equals(mediaOfAlbum.getPathMediaStock())) {
			// Cursor cursorVideo = context.getContentResolver().query(
			// MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
			// new String[] { MediaStore.Video.VideoColumns.DATA,
			// MediaStore.Video.VideoColumns._ID },
			// MediaStore.Video.VideoColumns.BUCKET_ID + " =?",
			// new String[] { String.valueOf(idAlbum) },
			// MediaStore.Video.VideoColumns.DATE_MODIFIED
			// + " DESC limit 3");
			// if (cursorVideo != null && cursorVideo.getCount() > 0) {
			// if (cursorVideo != null && cursorVideo.getCount() > 0) {
			// while (cursorVideo.moveToNext()) {
			// if (mediaOfAlbum.getPathMediaStock3() == null
			// || Config.NONE.equals(mediaOfAlbum
			// .getPathMediaStock3())) {
			// mediaOfAlbum
			// .setIdMedia3(cursorVideo.getLong(1));
			// mediaOfAlbum.setPathMediaStock3(cursorVideo
			// .getString(0));
			// mediaOfAlbum.setVideo3(true);
			// mediaOfAlbum.setIdMedia2(-1);
			// mediaOfAlbum.setPathMediaStock2(cursorVideo
			// .getString(0));
			// mediaOfAlbum.setVideo2(true);
			// mediaOfAlbum.setIdMedia(-1);
			// mediaOfAlbum.setPathMediaStock(Config.NONE);
			// mediaOfAlbum.setVideo(true);
			// } else if (mediaOfAlbum.getPathMediaStock2() == null
			// || Config.NONE.equals(mediaOfAlbum
			// .getPathMediaStock2())) {
			// mediaOfAlbum
			// .setIdMedia2(cursorVideo.getLong(1));
			// mediaOfAlbum.setPathMediaStock2(cursorVideo
			// .getString(0));
			// mediaOfAlbum.setVideo2(true);
			// mediaOfAlbum.setIdMedia(-1);
			// mediaOfAlbum.setPathMediaStock(Config.NONE);
			// mediaOfAlbum.setVideo(true);
			// } else if (mediaOfAlbum.getPathMediaStock() == null) {
			// mediaOfAlbum.setIdMedia(cursorVideo.getLong(1));
			// mediaOfAlbum.setPathMediaStock(cursorVideo
			// .getString(0));
			// mediaOfAlbum.setVideo(true);
			// }
			// }
			// }
			// }
			// tryClose(cursorVideo);
			// }

		}
		return mediaOfAlbum;
	}

	public ModelMediaOfAlbum getMediaOfAlbumVideo(long idAlbum) {
		ModelMediaOfAlbum mediaOfAlbum = new ModelMediaOfAlbum();
		Cursor cursor = context.getContentResolver().query(
				MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
				new String[] { MediaStore.Video.VideoColumns.DATA,
						MediaStore.Video.VideoColumns._ID },
				MediaStore.Video.VideoColumns.BUCKET_ID + " =?",
				new String[] { String.valueOf(idAlbum) },
				MediaStore.Video.VideoColumns.DATE_MODIFIED + " DESC limit 4");

		if (cursor != null && cursor.getCount() == 0) {
			mediaOfAlbum.setPathMediaStock4(Config.NONE);
			mediaOfAlbum.setIdMedia4(-1);
			mediaOfAlbum.setVideo4(false);
			mediaOfAlbum.setPathMediaStock3(Config.NONE);
			mediaOfAlbum.setIdMedia3(-1);
			mediaOfAlbum.setVideo3(false);
			mediaOfAlbum.setPathMediaStock2(Config.NONE);
			mediaOfAlbum.setIdMedia2(-1);
			mediaOfAlbum.setVideo2(false);
			mediaOfAlbum.setPathMediaStock(Config.NONE);
			mediaOfAlbum.setIdMedia(-1);
			mediaOfAlbum.setVideo(false);
		} else {
			if (cursor != null && cursor.getCount() > 0) {
				while (cursor.moveToNext()) {
					if (mediaOfAlbum.getPathMediaStock4() == null
							|| Config.NONE.equals(mediaOfAlbum
									.getPathMediaStock4())) {

						mediaOfAlbum.setPathMediaStock4(cursor.getString(0));
						mediaOfAlbum.setIdMedia4(cursor.getLong(1));
						mediaOfAlbum.setVideo4(true);

						mediaOfAlbum.setPathMediaStock3(Config.NONE);
						mediaOfAlbum.setIdMedia3(-1);
						mediaOfAlbum.setVideo3(false);

						mediaOfAlbum.setPathMediaStock2(Config.NONE);
						mediaOfAlbum.setIdMedia2(-1);
						mediaOfAlbum.setVideo2(false);

						mediaOfAlbum.setPathMediaStock(Config.NONE);
						mediaOfAlbum.setIdMedia(-1);
						mediaOfAlbum.setVideo(false);
					} else if (mediaOfAlbum.getPathMediaStock3() == null
							|| Config.NONE.equals(mediaOfAlbum
									.getPathMediaStock3())) {
						mediaOfAlbum.setPathMediaStock3(cursor.getString(0));
						mediaOfAlbum.setIdMedia3(cursor.getLong(1));
						mediaOfAlbum.setVideo3(true);
						mediaOfAlbum.setPathMediaStock2(Config.NONE);
						mediaOfAlbum.setIdMedia2(-1);
						mediaOfAlbum.setVideo2(false);
						mediaOfAlbum.setPathMediaStock(Config.NONE);
						mediaOfAlbum.setIdMedia(-1);
						mediaOfAlbum.setVideo(false);
					} else if (mediaOfAlbum.getPathMediaStock2() == null
							|| Config.NONE.equals(mediaOfAlbum
									.getPathMediaStock2())) {
						mediaOfAlbum.setPathMediaStock2(cursor.getString(0));
						mediaOfAlbum.setIdMedia2(cursor.getLong(1));
						mediaOfAlbum.setVideo2(true);
						mediaOfAlbum.setPathMediaStock(Config.NONE);
						mediaOfAlbum.setIdMedia(-1);
						mediaOfAlbum.setVideo(false);
					} else if (mediaOfAlbum.getPathMediaStock() == null
							|| Config.NONE.equals(mediaOfAlbum
									.getPathMediaStock())) {
						mediaOfAlbum.setPathMediaStock(cursor.getString(0));
						mediaOfAlbum.setIdMedia(cursor.getLong(1));
						mediaOfAlbum.setVideo(true);
					}
				}
			}
			tryClose(cursor);
		}
		return mediaOfAlbum;
	}

	private void tryClose(Cursor cursor) {
		try {
			cursor.close();
		} catch (Exception e) {
		}
	}

	@SuppressWarnings("deprecation")
	public ModelMedia getMediaByUri(Uri uri) {
		ModelMedia item = null;
		String path = null;
		if (uri != null) {
			path = URLDecoder.decode(uri.toString());
			if (path.indexOf("file://") != -1) {
				path = path.substring(7, path.length());
			} else {
				path = getFilePathFromContentUri(uri);
			}
			boolean isVideo = false;
			Cursor cursor = null;
			if (path != null) {
				if (Util.checkIsImage(path)) {
					cursor = context
							.getContentResolver()
							.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
									new String[] {
											MediaStore.Images.ImageColumns._ID,
											MediaStore.Images.ImageColumns.DATA,
											MediaStore.Images.ImageColumns.DISPLAY_NAME,
											MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
											MediaStore.Images.ImageColumns.DATE_MODIFIED },
									" " + MediaStore.Images.ImageColumns.DATA
											+ " = '" + path + "'  ", null, null);
					isVideo = false;
				} else if (Util.checkIsVideo(path)) {
					cursor = context
							.getContentResolver()
							.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
									new String[] {
											MediaStore.Video.VideoColumns._ID,
											MediaStore.Video.VideoColumns.DATA,
											MediaStore.Video.VideoColumns.DISPLAY_NAME,
											MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME,
											MediaStore.Video.VideoColumns.DATE_MODIFIED },
									" " + MediaStore.Video.VideoColumns.DATA
											+ " = '" + path + "'  ",
									null,
									MediaStore.Video.VideoColumns.DATE_MODIFIED
											+ " DESC ");
					isVideo = true;
				}
			}
			if (cursor != null && cursor.getCount() > 0)
				while (cursor.moveToNext()) {
					item = new ModelMedia();
					item.setIdMedia(cursor.getLong(0));
					item.setNameAlbum(cursor.getString(3));
					item.setPathMediaStock(cursor.getString(1));
					item.setNameMedia(cursor.getString(2));
					item.setDate(cursor.getString(4));
					item.setVideo(isVideo);
				}
			tryClose(cursor);
		}
		return item;
	}

	public String getFilePathFromContentUri(Uri uri) {
		String filePath;
		String[] filePathColumn = { MediaColumns.DATA };
		Cursor cursor = context.getContentResolver().query(uri, filePathColumn,
				null, null, null);
		cursor.moveToFirst();
		int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
		filePath = cursor.getString(columnIndex);
		tryClose(cursor);
		return filePath;
	}

}
